import { defineStore } from 'pinia';
import { lStorage } from '@/utils/cache';
import { initThemeSettings } from './helpers';

export const useThemeStore = defineStore('theme-store', {
	state: () => initThemeSettings(),
	getters: {},
	actions: {
		/** 重置主题状态 */
		resetThemeStore() {
			lStorage.remove('themeSettings');
			this.$reset();
		},
		/** 缓存主题配置 */
		cacheThemeSettings() {
			const isProd = import.meta.env.PROD;
			if (isProd) {
				lStorage.set('themeSettings', this.$state);
			}
		},
		/** 设置主题模式 **/
		setThemeMode(mode) {
			this.themeMode = mode;
			this.changeMode(mode);
		},
		/** 切换主题模式 **/
		changeMode(mode) {
			console.log(mode);
			let theme = mode;
			if (mode === 'theme-auto') {
				const media = window.matchMedia('(prefers-color-scheme:dark)');
				if (media.matches) {
					theme = 'theme-dark';
				} else {
					theme = 'theme-light';
				}
			}
			const isDarkMode = theme === 'theme-dark';
			document.body.setAttribute('arco-theme', isDarkMode ? 'dark' : '');
		},
		/** 设置布局模式 */
		setLayoutMode(mode) {
			this.layout.mode = mode;
		},
		/** 侧边栏宽度 */
		setSiderWidth(width) {
			if (width) {
				this.sider.width = width;
			}
		},
	},
});
